//
//  ChangePswViewController.m
//  FeiYang
//
//  Created by user on 15/10/14.
//  Copyright © 2015年 feiyang. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "LoginViewController.h"

@interface ChangePwdViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UITableView  *changePswTab;
@property(nonatomic,strong) UIButton*changePswBtn ;
@end

@implementation ChangePwdViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloginView) name:@"reLogin" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"修改密码"];
    [self.view addSubview:topView];
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 35.0f, 49.0f, 16.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
    [self createUI];
}
- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardhide)];
    tapGesture.cancelsTouchesInView = NO;
    [self.changePswTab addGestureRecognizer:tapGesture];
    
    
    
    [self.view addSubview:self.changePswTab];
    [self.changePswTab addSubview:self.changePswBtn];
}
-(UITableView *)changePswTab
{
    if (!_changePswTab) {
        
        _changePswTab = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _changePswTab.delegate = self;
        _changePswTab.dataSource = self;
        _changePswTab.rowHeight = 50.0f;
        _changePswTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _changePswTab;
}
//下一步按钮
-(UIButton *)changePswBtn
{
    if (!_changePswBtn) {
        _changePswBtn = [[UIButton alloc] initWithFrame:CGRectMake(16.0f,214.0f, SCREEN_WIDTH-32.0f, 44.0f)];
        [_changePswBtn addTarget:self action:@selector(changePswBtnTapped) forControlEvents:UIControlEventTouchUpInside];
        [_changePswBtn setTitle:@"修改密码" forState:UIControlStateNormal];
        _changePswBtn.layer.cornerRadius = 2;
        [_changePswBtn setBackgroundColor:[UIColor hexStringToColor:COLOR_VALUE] forState:UIControlStateNormal];
        
    }
    return _changePswBtn;
}
- (void)changePswBtnTapped
{
    //重置密码
    if ([__textField.text isEqualToString:@""]) {
        ALERT_VIEW(@"请填写原密码");
        return;
    }
    if ([__textFieldPwd.text isEqualToString:@""]) {
        ALERT_VIEW(@"请填写新密码");
        return;
    }
    if ([__textFieldConfirmPwd.text isEqualToString:@""]) {
        ALERT_VIEW(@"请填写确认密码");
        return;
    }

    if (![__textFieldPwd.text isEqualToString:__textFieldConfirmPwd.text]) {
        ALERT_VIEW(@"新密码和确认密码不一致");
        return;
    }
    
    if ([AccountTools validatePassWord:__textFieldPwd.text]==NO) {
        ALERT_VIEW(@"密码为8~32位字符");
        return;
    }
    [[AnimationIndicator sharedIndicator] startAnimation];
    [AppGlobalClass PostDataWithNSstring:UPDATEPWD withDictionary:@{@"token":[AccountTools getUserToken],@"oldPassword":__textField.text,@"newPassword":__textFieldPwd.text} success:^(NSDictionary *dictionary) {
        
        
        
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            
            ALERT_VIEW(@"密码重置成功");
            
            [self.navigationController popViewControllerAnimated:TRUE];
            
            
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            
            ALERT_VIEW(str);
            
        }
        
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"完成" withType:TRUE];
    } failure:^(NSError *error) {
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"失败" withType:TRUE];
        
    } networkerror:^(NSString *networkerror) {
        
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"无网络" withType:TRUE];
    }];
    
    
    
    
}

- (void)keyboardhide
{
    [__textField resignFirstResponder];
    [__textFieldPwd resignFirstResponder];
    [__textFieldConfirmPwd resignFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
static UITextField *__textField;//记录当前旧密码的textField
static UITextField *__textFieldPwd;//记录当前新密码的textField
static UITextField *__textFieldConfirmPwd;//记录当前确认密码的textField
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *sepLab = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 49.5f, SCREEN_WIDTH-16.0f, 0.2f)];
        sepLab.backgroundColor = [UIColor lightGrayColor];
        [cell addSubview:sepLab];
        
        UITextField *userTF = [[UITextField alloc] init];
        userTF.tag = 2000;
        userTF.delegate = self;
        userTF.secureTextEntry = YES;
        userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        userTF.font = [UIFont systemFontOfSize:14.0f];
        userTF.frame = CGRectMake(100.0f, 5.0f, SCREEN_WIDTH-32.0f, 44.0f);
        [cell.contentView addSubview:userTF];
        
        
    }
    
    UITextField *textF = (UITextField *)[cell.contentView viewWithTag:2000];
    NSArray *textArr = @[@"原密码",@"新密码",@"确认密码"];
    NSArray *placrArr = @[@"请输入您的旧密码",@"请输入您的新密码",@"请再次输入您的密码"];

    cell.textLabel.text = textArr[indexPath.row];
    textF.placeholder =placrArr[indexPath.row];
    switch (indexPath.row) {
        case 0:
            __textField = textF;
            break;
        case 1:
            __textFieldPwd = textF;
            break;
        case 2:
            __textFieldConfirmPwd = textF;
            break;
        default:
            break;
    }

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 16.0f;
}

-(void)reloginView
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号在另一台机器登录，请重新登录!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}
#pragma mark alertDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [AccountTools exitLogin];
    
    LoginViewController *loginViewCon = [[LoginViewController alloc]init];
    [self presentViewController:loginViewCon animated:TRUE completion:^{}];
    [self.navigationController popViewControllerAnimated:FALSE];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self keyboardhide];
    return YES;
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
